package activemq_chap7;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {
	private static ActiveMQConnectionFactory factory;
	private static Connection connection;
	private static Session session;
	private static MessageProducer producer;

	public void sendMessage() {
		try {
			factory = new ActiveMQConnectionFactory(
					ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
			connection = factory.createConnection("admin", "admin");
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = session.createQueue("tinhcd");
			producer = session.createProducer(queue);
			TextMessage textMessage = session.createTextMessage();
			textMessage.setText("This message send from Sender class !");
			producer.send(textMessage);
			System.out.println("Send message success !");
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		Sender sender = new Sender();
		sender.sendMessage();
	}
}
